package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.ChannelCallCfgMapper;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 通道服务调用配置表操作接口实现类
 * Created by wangcheng on 2021/2/25 17:10
 */
public class ChannelCallCfgMapperImpl implements ChannelCallCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public ChannelCallCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertChannelCallCfg(ChannelCallCfg channelCallCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertChannelCallCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, channelCallCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记通道服务调用配置失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,bizScene:<{}>,callIndex:<{}>,iReturnCode:<{}>",
                    channelCallCfg.getInstId(), channelCallCfg.getMsgSrcId(), channelCallCfg.getTxnNum(), channelCallCfg.getBizType(), channelCallCfg.getBizScene(), channelCallCfg.getCallIndex(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateChannelCallCfgByPrimaryKey(ChannelCallCfg channelCallCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateChannelCallCfgByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, channelCallCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新通道服务调用配置失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,bizScene:<{}>,callIndex:<{}>,iReturnCode:<{}>",
                    channelCallCfg.getInstId(), channelCallCfg.getMsgSrcId(), channelCallCfg.getTxnNum(), channelCallCfg.getBizType(), channelCallCfg.getBizScene(), channelCallCfg.getCallIndex(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ChannelCallCfg selectChannelCallCfgByPrimaryKey(String instId, String primaryKey) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectChannelCallCfgByPrimaryKey");
        ChannelCallCfg channelCallCfg = new ChannelCallCfg();
        channelCallCfg.setInstId(instId);
        channelCallCfg.setPrimaryKey(primaryKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, channelCallCfg, channelCallCfg);
        if (0 != iReturnCode) {
            rglog.error("根据主键查询通道服务调用配置失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", instId, primaryKey, iReturnCode);
            return null;
        }
        return channelCallCfg;
    }

    @Override
    public List<ChannelCallCfg> selectChannelCallCfgByBizSceneAndCallIndex(String instId, String msgSrcId, String transChannelId, String txnNum, String bizType, String bizScene, String callIndex) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectChannelCallCfgByBizSceneAndCallIndex");
        ChannelCallCfg channelCallCfg = new ChannelCallCfg();
        channelCallCfg.setInstId(instId);
        channelCallCfg.setMsgSrcId(msgSrcId);
        channelCallCfg.setTransChannelId(transChannelId);
        channelCallCfg.setTxnNum(txnNum);
        channelCallCfg.setBizType(bizType);
        channelCallCfg.setBizScene(bizScene);
        channelCallCfg.setCallIndex(callIndex);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, channelCallCfg, ChannelCallCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据业务场景及调用序号查询通道服务调用配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,bizScene:<{}>,callIndex:<{}>,iReturnCode:<{}>",
                    instId, msgSrcId, txnNum, bizType, bizScene, callIndex);
            return null;
        }
        return objectList.stream().map(item -> (ChannelCallCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public List<ChannelCallCfg> selectChannelCallCfgByTxnNum(String instId, String msgSrcId, String transChannelId, String txnNum, String bizType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectChannelCallCfgByTxnNum");
        ChannelCallCfg channelCallCfg = new ChannelCallCfg();
        channelCallCfg.setInstId(instId);
        channelCallCfg.setMsgSrcId(msgSrcId);
        channelCallCfg.setTransChannelId(transChannelId);
        channelCallCfg.setTxnNum(txnNum);
        channelCallCfg.setBizType(bizType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, channelCallCfg, ChannelCallCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据交易码查询通道服务调用配置失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>", instId, msgSrcId, txnNum, bizType);
            return null;
        }
        return objectList.stream().map(item -> (ChannelCallCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public List<ChannelCallCfg> loadChannelCallCfg(String dbPoolCacheNodeName, String instId, String msgSrcId, String transChannelId, String txnNum, String bizType) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCacheNodeName, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectChannelCallCfgByTxnNum");
        ChannelCallCfg channelCallCfg = new ChannelCallCfg();
        channelCallCfg.setInstId(instId);
        channelCallCfg.setMsgSrcId(msgSrcId);
        channelCallCfg.setTransChannelId(transChannelId);
        channelCallCfg.setTxnNum(txnNum);
        channelCallCfg.setBizType(bizType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, channelCallCfg, ChannelCallCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据交易码加载通道服务调用配置失败,poolName:<{}>,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>", dbPoolCacheNodeName, instId, msgSrcId, txnNum, bizType);
            return null;
        }
        return objectList.stream().map(item -> (ChannelCallCfg) (item)).collect(Collectors.toList());
    }
}
